package com.darren.befriend.fragments;

import java.util.ArrayList;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.darren.befriend.BaseActivity;
import com.darren.befriend.HomeActivity;
import com.darren.befriend.R;
import com.darren.befriend.UserSettingInitialActivity;
import com.darren.befriend.utilities.LogUtil;
import com.darren.befriend.utilities.Utilities;
import com.darren.befriend.views.GenderSegment;
import com.darren.befriend.views.GenderSegment.OnGenderChangeListener;
import com.darren.befriend.views.PickerDialog;
import com.darren.befriend.views.PickerDialog.OnDoneClickListener;
import com.darren.befriend.views.UserSettingItemView;
import com.darren.befriend.views.UserSettingItemView.OnCellDirectListener;
import com.parse.ParseObject;
import com.parse.ParseUser;

public class UserSettingFragment extends Fragment implements
		OnSeekBarChangeListener {

	private ViewGroup mainView;
	private UserSettingItemView mFriendFor;
	private UserSettingItemView mMyAge;
	private UserSettingItemView mPersonality;
	private UserSettingItemView mChildren;
	private UserSettingItemView mInterest;
	private UserSettingItemView mSport;
	private UserSettingItemView mFilm;
	private UserSettingItemView mMusic;
	private UserSettingItemView mAboutMe;
	private UserSettingItemView mUniveristy;
	private UserSettingItemView mEmployer;
	private GenderSegment mGenderSeg;
	private SeekBar mMoneySeekbar;
	private TextView mMoneyValue;
	private final int MIN_AGE = 18;
	private final int MAX_AGE = 64;
	private int mAge;
	private String[] personalities;
	private String[] mAgeStringArr;
	private String[] mChildrenArr;
	private ArrayList<String> mUniversityArr;
	private ParseUser mUser;
	private ParseObject meInfo;

	public static Fragment create() {
		UserSettingFragment fragment = new UserSettingFragment();
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		mainView = (ViewGroup) inflater.inflate(R.layout.layout_me_fragment,
				container, false);

		try {

			((HomeActivity) getActivity()).setContent(this);
		} catch (Exception ex) {

			LogUtil.e("UserSettingFragment", ex.getMessage());
		}

		initViews();
		return mainView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		if (getActivity() == null) {

			return;
		}

		String title = getString(R.string.me);
		if (getActivity() instanceof UserSettingInitialActivity) {

			((UserSettingInitialActivity) getActivity())
					.setActivityTitle(title);
			((UserSettingInitialActivity) getActivity()).setNavigationImage(
					R.drawable.item_music_b, R.drawable.item_done);
			((UserSettingInitialActivity) getActivity()).pushTitle(title);
		} else if (getActivity() instanceof HomeActivity) {

			((HomeActivity) getActivity()).showRightButtonImg(false);
			((HomeActivity) getActivity()).setLeftImage(R.drawable.item_back);
			((HomeActivity) getActivity()).setActivityTitle(title);
		}

		if (getActivity() instanceof BaseActivity) {

			mUser = ((BaseActivity) getActivity()).getCurrentUser();
			if (mUser == null) {

				return;
			}

			meInfo = (ParseObject) mUser.get("MeInfo");
			if (meInfo == null) {

				return;
			}

			initData();
		}
	}

	@Override
	public void onPause() {
		super.onPause();

	}

	/**
	 * initViews
	 */
	private void initViews() {

		mFriendFor = (UserSettingItemView) mainView
				.findViewById(R.id.friend_for_container);
		mFriendFor.setLeftTitle(getString(R.string.friend_for));
		mFriendFor.setOnCellDirectListener(new OnCellDirectListener() {

			@Override
			public void onCellClick() {
				if (getActivity() != null
						&& (getActivity() instanceof UserSettingInitialActivity || getActivity() instanceof HomeActivity)) {
					Fragment fragment = FriendForFragment.create();
					if (getActivity() instanceof UserSettingInitialActivity) {

						((UserSettingInitialActivity) getActivity())
								.addFragment(fragment);
					} else if (getActivity() instanceof HomeActivity) {

						((HomeActivity) getActivity())
								.switchContentWithNormalAnimation(fragment);
					}
				}
			}
		});

		mMyAge = (UserSettingItemView) mainView
				.findViewById(R.id.age_container);
		mMyAge.setLeftTitle(getString(R.string.my_age));
		mMyAge.setOnCellDirectListener(new OnCellDirectListener() {

			@Override
			public void onCellClick() {
				mAgeStringArr = new String[MAX_AGE - MIN_AGE + 1];
				for (int i = MIN_AGE; i <= MAX_AGE; i++) {

					mAgeStringArr[i - MIN_AGE] = String.valueOf(i);
				}

				PickerDialog agePicker = new PickerDialog(getActivity(),
						mAgeStringArr, 0, (MAX_AGE - MIN_AGE));
				agePicker.setOnDoneClickListener(new OnDoneClickListener() {

					@Override
					public void OnDone(int position, String value) {

						mAge = position + MIN_AGE;
						meInfo.put("MyAge", mAge);
						if (position <= (MAX_AGE - MIN_AGE)) {

							mMyAge.setRightTitle(String.valueOf(mAge)
									+ " years");
						} else {

							mMyAge.setRightTitle("64+ years");
						}
					}
				});

				agePicker.show();
			}
		});

		mPersonality = (UserSettingItemView) mainView
				.findViewById(R.id.personality_container);
		mPersonality.setLeftTitle(getString(R.string.personality));
		mPersonality.setOnCellDirectListener(new OnCellDirectListener() {

			@Override
			public void onCellClick() {
				PickerDialog personalityPicker = new PickerDialog(
						getActivity(), personalities, 0,
						personalities.length - 1);
				personalityPicker
						.setOnDoneClickListener(new OnDoneClickListener() {

							@Override
							public void OnDone(int position, String value) {
								meInfo.put("Personality", position);
								mPersonality.setRightTitle(value);
							}
						});
				personalityPicker.show();
			}
		});

		mChildren = (UserSettingItemView) mainView
				.findViewById(R.id.children_container);
		mChildren.setLeftTitle(getString(R.string.children));
		mChildren.setOnCellDirectListener(new OnCellDirectListener() {

			@Override
			public void onCellClick() {
				PickerDialog childrenPicker = new PickerDialog(getActivity(),
						mChildrenArr, 0, mChildrenArr.length - 1);
				childrenPicker
						.setOnDoneClickListener(new OnDoneClickListener() {

							@Override
							public void OnDone(int position, String value) {
								meInfo.put("Children", position);
								mChildren.setRightTitle(value);
							}
						});
				childrenPicker.show();
			}
		});

		mInterest = (UserSettingItemView) mainView
				.findViewById(R.id.interest_container);
		mInterest.setLeftTitle(getString(R.string.interest));
		mInterest.setOnCellDirectListener(new OnCellDirectListener() {

			@Override
			public void onCellClick() {
				if (getActivity() != null
						&& (getActivity() instanceof UserSettingInitialActivity || getActivity() instanceof HomeActivity)) {
					Fragment fragment = InterestFragment
							.create(HobbyBaseFragment.SECOND_MODE);
					if (getActivity() instanceof UserSettingInitialActivity) {
						((UserSettingInitialActivity) getActivity())
								.addFragment(fragment);
					} else if (getActivity() instanceof HomeActivity) {
						((HomeActivity) getActivity())
								.switchContentWithNormalAnimation(fragment);
					}
				}
			}
		});

		mSport = (UserSettingItemView) mainView
				.findViewById(R.id.sport_container);
		mSport.setLeftTitle(getString(R.string.sports));
		mSport.setOnCellDirectListener(new OnCellDirectListener() {

			@Override
			public void onCellClick() {
				if (getActivity() != null
						&& (getActivity() instanceof UserSettingInitialActivity || getActivity() instanceof HomeActivity)) {
					Fragment fragment = SportsFragment
							.create(HobbyBaseFragment.SECOND_MODE);
					if (getActivity() instanceof UserSettingInitialActivity) {
						((UserSettingInitialActivity) getActivity())
								.addFragment(fragment);
					} else if (getActivity() instanceof HomeActivity) {
						((HomeActivity) getActivity())
								.switchContentWithNormalAnimation(fragment);
					}
				}
			}
		});

		mFilm = (UserSettingItemView) mainView
				.findViewById(R.id.film_container);
		mFilm.setLeftTitle(getString(R.string.film));
		mFilm.setOnCellDirectListener(new OnCellDirectListener() {

			@Override
			public void onCellClick() {
				if (getActivity() != null
						&& (getActivity() instanceof UserSettingInitialActivity || getActivity() instanceof HomeActivity)) {
					Fragment fragment = FilmFragment
							.create(HobbyBaseFragment.SECOND_MODE);
					if (getActivity() instanceof UserSettingInitialActivity) {
						((UserSettingInitialActivity) getActivity())
								.addFragment(fragment);
					} else if (getActivity() instanceof HomeActivity) {
						((HomeActivity) getActivity())
								.switchContentWithNormalAnimation(fragment);
					}
				}
			}
		});

		mMusic = (UserSettingItemView) mainView
				.findViewById(R.id.music_container);
		mMusic.setLeftTitle(getString(R.string.music));
		mMusic.setOnCellDirectListener(new OnCellDirectListener() {

			@Override
			public void onCellClick() {
				if (getActivity() != null
						&& (getActivity() instanceof UserSettingInitialActivity || getActivity() instanceof HomeActivity)) {
					Fragment fragment = MusicFragment
							.create(HobbyBaseFragment.SECOND_MODE);
					if (getActivity() instanceof UserSettingInitialActivity) {
						((UserSettingInitialActivity) getActivity())
								.addFragment(fragment);
					} else if (getActivity() instanceof HomeActivity) {
						((HomeActivity) getActivity())
								.switchContentWithNormalAnimation(fragment);
					}
				}
			}
		});

		mAboutMe = (UserSettingItemView) mainView
				.findViewById(R.id.about_container);
		mAboutMe.setLeftTitle(getString(R.string.about));
		mAboutMe.setOnCellDirectListener(new OnCellDirectListener() {

			@Override
			public void onCellClick() {
				if (getActivity() != null
						&& (getActivity() instanceof UserSettingInitialActivity || getActivity() instanceof HomeActivity)) {
					Fragment fragment = AboutMeFragment.create();
					if (getActivity() instanceof UserSettingInitialActivity) {

						((UserSettingInitialActivity) getActivity())
								.addFragment(fragment);
					} else if (getActivity() instanceof HomeActivity) {

						((HomeActivity) getActivity())
								.switchContentWithNormalAnimation(fragment);
					}
				}
			}
		});

		mUniveristy = (UserSettingItemView) mainView
				.findViewById(R.id.uninersity_container);
		mUniveristy.setLeftTitle(getString(R.string.university));
		mUniveristy.setOnCellDirectListener(new OnCellDirectListener() {

			@Override
			public void onCellClick() {
				if (getActivity() != null
						&& (getActivity() instanceof UserSettingInitialActivity || getActivity() instanceof HomeActivity)) {
					Fragment fragment = UniversityFragment
							.create(mUniversityArr);
					if (getActivity() instanceof UserSettingInitialActivity) {

						((UserSettingInitialActivity) getActivity())
								.addFragment(fragment);
					} else if (getActivity() instanceof HomeActivity) {

						((HomeActivity) getActivity())
								.switchContentWithNormalAnimation(fragment);
					}
				}
			}
		});

		mEmployer = (UserSettingItemView) mainView.findViewById(R.id.employer);
		mEmployer.setLeftTitle(getString(R.string.employer));
		mEmployer.setOnCellDirectListener(new OnCellDirectListener() {

			@Override
			public void onCellClick() {
				final Dialog employerDialog = new Dialog(getActivity());
				employerDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
				employerDialog.setContentView(R.layout.layout_employer_dialog);
				employerDialog.getWindow().setLayout(
						WindowManager.LayoutParams.MATCH_PARENT,
						getResources().getDimensionPixelSize(
								R.dimen.comm_size_200dp));
				employerDialog.findViewById(R.id.ok_lbl).setOnClickListener(
						new OnClickListener() {

							@Override
							public void onClick(View v) {
								EditText employerEdt = (EditText) employerDialog
										.findViewById(R.id.employer_edt);
								String employer = employerEdt.getText()
										.toString();
								meInfo.put("Employer", employer);
								mEmployer.setRightTitle(employer);
								employerDialog.dismiss();
							}
						});
				employerDialog.show();
			}
		});

		mGenderSeg = (GenderSegment) mainView
				.findViewById(R.id.gender_container);
		mGenderSeg.setOnGenderListener(new OnGenderChangeListener() {

			@Override
			public void onChange(boolean isLeftSelected) {
				if (isLeftSelected) {
					meInfo.put("Gender", 0);
				} else {
					meInfo.put("Gender", 1);
				}
			}
		});

		mMoneySeekbar = (SeekBar) mainView.findViewById(R.id.money_seekbar);
		mMoneySeekbar.setOnSeekBarChangeListener(this);
		mMoneyValue = (TextView) mainView.findViewById(R.id.money_value);
	}

	/**
	 * init data
	 */
	private void initData() {
		personalities = new String[] { "Adventurous", "Care Free", "Confident",
				"Easy going", "Energetic", "Funny", "Generous", "Helpful",
				"Quiet", "Reserved", "Shy", "Spontaneous", "Sensitive" };
		// religions = new String[] { "Christian", "Islamic", "Secular",
		// "Nonreligious", "Agnostic", "Atheist", "Hindu", "Buddhist",
		// "Sikhi", "Jewish" };
		// intelligence = new String[] { "Below Average", "Average",
		// "Above Average", "High" };
		mChildrenArr = new String[] { "0", "1", "2", "3", "4", "5", "6", "7",
				"8", "9", "10" };
		mUniversityArr = Utilities.readFileAssest(getActivity(),
				"university.txt");
		if (meInfo.isDataAvailable()) {
			int genderIndex = (Integer) meInfo.getInt("Gender");
			if (genderIndex == 0) {
				mGenderSeg.setLeftSelected(true);
			} else if (genderIndex == 1) {
				mGenderSeg.setLeftSelected(false);
			}
			//
			int burnValue = meInfo.getInt("MoneyToBurn");
			mMoneySeekbar.setProgress(burnValue);
			if (burnValue > 1000) {
				mMoneyValue.setText("1000+");
			} else {
				mMoneyValue.setText(String.format("%d", burnValue));
			}
			//
			mEmployer.setRightTitle(meInfo.get("Employer").toString());
			LogUtil.e("UserSettingFragment", meInfo.get("University")
					.toString());
			mUniveristy.setRightTitle(mUniversityArr.get(meInfo
					.getInt("University")));
			mMyAge.setRightTitle(meInfo.get("MyAge").toString());
			mPersonality.setRightTitle(personalities[meInfo
					.getInt("Personality")]);
			mChildren.setRightTitle(String.valueOf(meInfo.getInt("Children")));

		}
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		mMoneyValue.setText(String.valueOf(progress));
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {

	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		meInfo.put("MoneyToBurn", seekBar.getProgress());
	}
}
